﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Fawcett.Plugins.Controls
{


    public partial class DualPushButton: UserControl
    {
        public enum State { ON, OFF }

        public SolidBrush backoff { get; set; }

        public SolidBrush backon { get; set; }
        public SolidBrush forebrush { get; set; }
        public SolidBrush onbrush { get; set; }
        public State button_state { get; set; }
        public String text { get; set; }
        public Font font { get; set; }


        public DualPushButton()
        {
            SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint |
            ControlStyles.AllPaintingInWmPaint, true);

            font = new System.Drawing.Font("Arial", 16); 
            backoff = new SolidBrush(Color.Black);
            backon = new SolidBrush(Color.Black);
            forebrush = new SolidBrush(Color.Red);
            onbrush = new SolidBrush(Color.White);
            button_state = State.OFF;

            InitializeComponent();
        }


        Bitmap bmpBackground = new Bitmap( Images.DualPushButton );
        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);

            float scale = (float)this.Width / bmpBackground.Width;

            bmpBackground.MakeTransparent(Color.Yellow);

            // diplay mask
            Pen maskPen = new Pen(this.BackColor, 30 * scale);
            pe.Graphics.DrawRectangle(maskPen, 0, 0, bmpBackground.Width * scale, bmpBackground.Height * scale);

            DrawMessageBox(pe, scale);

            pe.Graphics.DrawImage(bmpBackground, 0, 0, (float)(bmpBackground.Width * scale), (float)(bmpBackground.Height * scale));


                

        }

        protected void DrawMessageBox(PaintEventArgs pe,  float scale)
        {
            RectangleF physicalPosition = new RectangleF();

            physicalPosition.X = Convert.ToInt32(55 * scale);
            physicalPosition.Y = Convert.ToInt32(32 * scale);
            physicalPosition.Height = Convert.ToInt32(200 * scale);
            physicalPosition.Width = Convert.ToInt32(200 * scale);


            pe.Graphics.FillRectangle((button_state == State.ON) ? backon : backoff, physicalPosition);

            Font temp = new Font( font.Name ,  font.Size * (scale *2) );

            StringFormat sf = new StringFormat();
            sf.LineAlignment = StringAlignment.Center;
            sf.Alignment = StringAlignment.Center;

            pe.Graphics.DrawString(text, 
                                   temp,
                                   (button_state == State.ON) ? onbrush : forebrush, 
                                   physicalPosition, sf);

        }

        delegate void RefreshCallback();
         protected  void safeRefresh()
        {
            if (this.InvokeRequired)
            {
                RefreshCallback d = new RefreshCallback(safeRefresh);
                this.Invoke(d, new object[]{} );
            }
            else
            {
                this.Refresh();
            }
        }

        public void Toggle( State on_off)
        {
            button_state = on_off;

            this.safeRefresh();
        }



    }

    public static class BrushHelper
    {
        public static float Lerp(this float start, float end, float amount)
        {
            float difference = end - start;
            float adjusted = difference * amount;
            return start + adjusted;
        }
        public static Color Lerp(this Color colour, Color to, float amount)
        {
            // start colours as lerp-able floats
            float sr = colour.R, sg = colour.G, sb = colour.B;

            // end colours as lerp-able floats
            float er = to.R, eg = to.G, eb = to.B;

            // lerp the colours to get the difference
            byte r = (byte)sr.Lerp(er, amount),
                 g = (byte)sg.Lerp(eg, amount),
                 b = (byte)sb.Lerp(eb, amount);

            // return the new colour
            return Color.FromArgb(r, g, b);
        }

        public static SolidBrush Dim(this SolidBrush input)
        {
            Color col = input.Color;

            col.Lerp(Color.Green, 0.80f);

            return new SolidBrush(col);
        }

        public static SolidBrush Colour(this SolidBrush input, Color c)
        {
            return new SolidBrush(c); ;
        }

    }
}
